import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_common/components/widget/ntt_gesture_widget.dart';
import 'package:lib_common/components/widget/ntt_image_view.dart';
import 'package:lib_common/r.dart';

import '../widget/ntt_ordinary_button.dart';

/// 选择弹窗
/// 支持多选与单选
class NttSelectedDialog extends StatelessWidget {
  /// 标题
  final String? title;

  /// 数据源
  final List<String> datas;

  /// 按钮文本
  final String? buttonText;

  /// 选中的数据
  late final List<String> _selectsData;

  /// 刷新状态
  final Map<String, StateSetter> _statesMap = {};

  /// 是否单选
  final bool isSingleSelect;

  NttSelectedDialog({
    super.key,
    required this.title,
    required this.datas,
    this.buttonText,
    this.isSingleSelect = false,
    List<String>? defaultSelects,
  }){
    if(isSingleSelect){
      _selectsData = [datas.first];
    }else{
      _selectsData = defaultSelects ?? [];
    }
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      onPopInvoked: (didPop) {
        if (!didPop) {
          Navigator.of(context).pop(false);
        }
      },
      canPop: false,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
          child: Container(
            width: 640.w,
            constraints: BoxConstraints(maxHeight: 0.7.sh),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(56.h),
            ),
            padding:
            EdgeInsets.only(top: 30.h, bottom: 32.h, left: 32.w, right: 32.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  title ?? "标题",
                  style:
                  TextStyle(fontSize: 40.h, color: const Color(0xffFDAD30)),
                ),
                SizedBox(
                  height: 24.h,
                ),
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                      color: const Color(0xffF4F6FA),
                      borderRadius: BorderRadius.circular(24.r),
                    ),
                    child: ListView.builder(
                      padding: EdgeInsets.zero,
                      itemBuilder: (BuildContext context, int index) {
                        return NttGestureWidget(
                          onTap: () {
                            var item = datas[index];
                            debugPrint("点击了 $item , isSingleSelect -> $isSingleSelect , _selectsData -> $_selectsData");
                            if(isSingleSelect){
                              _selectsData.removeLast();
                              _selectsData.add(item);
                              _statesMap.forEach((key, value) {
                                value.call(() {});
                              });
                            }else{
                              if (_selectsData.contains(item)) {
                                _selectsData.remove(item);
                              } else {
                                _selectsData.add(item);
                              }
                              _statesMap[item]?.call(() {});
                            }

                          },
                          behavior: HitTestBehavior.opaque,
                          child: Container(
                            height: 120.h.toInt().toDouble(),
                            padding: EdgeInsets.symmetric(horizontal: 32.w),
                            child: Row(
                              children: [
                                Expanded(
                                  child: Text(
                                    datas[index],
                                    style: TextStyle(
                                      fontSize: 36.r,
                                      color: const Color(0xff495166),
                                    ),
                                  ),
                                ),
                                SizedBox(
                                  width: 24.w,
                                ),
                                StatefulBuilder(builder:
                                    (BuildContext context, StateSetter setState) {
                                  _statesMap[datas[index]] = setState;
                                  return NttImageView(
                                    imageUrl: _selectsData.contains(datas[index])
                                        ? R.icon_selected_1_png
                                        : R.icon_toast_1_png,
                                    width: 44.w,
                                    height: 44.w,
                                  );
                                }),
                              ],
                            ),
                          ),
                        );
                      },
                      itemCount: datas.length,
                    ),
                  ),
                ),
                SizedBox(
                  height: 24.h,
                ),
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    NttOrdinaryButton(
                      buttonText ?? "保存",
                      width: 280.w,
                      height: 96.h,
                      textSize: 36.r,
                      textColor: Colors.white,
                      radius: 100.w,
                      onTap: () {
                        Navigator.of(context).pop({
                          "selects": _selectsData,
                        });
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
